package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.db.UserInfo;
import com.itheima.mapper.AnnouncementMapper;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.service.db.UserInfoService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private AnnouncementMapper announcementMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findByUserId(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    @Override
    public PageBeanVo findUserList(Integer pageNum, Integer pageSize) {
        //1. 创建Page对象
        IPage<UserInfo> page = new Page<>(pageNum, pageSize);

        //2. 创建查询条件
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();

        //3. 执行查询
        page = userInfoMapper.selectPage(page, wrapper);

        //4. 返回对象
        return new PageBeanVo(pageNum, pageSize, page.getTotal(), page.getRecords());
    }

    //公告
    @Override
    public PageBeanVo announcementList(Integer pageNum, Integer pageSize) {
        //创建Page对象
        IPage<Announcement> page = new Page<>(pageNum, pageSize);
        //创建查询条件
        QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
        //执行查询条件
        page = announcementMapper.selectPage(page, wrapper);
        //返回对象
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }
}
